package com.pronhub.luowowo.website.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.pronhub.luowowo.article.domain.Destination;
import com.pronhub.luowowo.article.domain.Region;
import com.pronhub.luowowo.article.domain.StrategyCatalog;
import com.pronhub.luowowo.article.domain.StrategyDetail;
import com.pronhub.luowowo.article.query.StrategyCatalogQuery;
import com.pronhub.luowowo.article.query.TravelQuery;
import com.pronhub.luowowo.article.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("destination")
public class DestinationController {

    @Reference
    private IRegionService regionService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private ITravelService travelService;

    @RequestMapping("")
    public String indesx(Model model){
        //获得热门地区
        List<Region> regions = regionService.queryHotRegions();
        model.addAttribute("hotRegions",regions);
        return "destination/index";
    }

    @RequestMapping("getHotDestByRegionId")
    public String getHotDestByRegionId(Long regionId,Model model){
        //获得相对应的地区的集合还有他的儿子
        List<Destination> destByRegionId = destinationService.getDestByRegionId(regionId);
        //进行相对应拼接 渲染相对应的页面 然后把整个页面再返回回去
        List<Destination> left = destByRegionId.subList(0,destByRegionId.size()/2+1);
        List<Destination> right = destByRegionId.subList(destByRegionId.size()/2+1,destByRegionId.size());

        model.addAttribute("leftDests",left);
        model.addAttribute("rightDests",right);
        model.addAttribute("regionId",regionId);
        return "destination/hotdestTpl";
    }

    @RequestMapping("guide")
    public String guide(Long id,Model model){
        //一出最后一名并且返回被一出的值
        List<Destination> toasts = destinationService.getToasts(id);
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);

        //通过dest_id来查攻略的分类
        List<StrategyCatalog> catalogs = strategyCatalogService.queryByDestId(id);
        model.addAttribute("catalogs",catalogs);
        //目的地前三篇点击数的文章
        List<StrategyDetail> strategyDetails = strategyDetailService.queryViewTop3(id);
        model.addAttribute("strategyDetails",strategyDetails);

        return "destination/guide";
    }
    //surveyPage
    @RequestMapping("surveyPage")
    public String surveyPage(Model model, @ModelAttribute("qo")StrategyCatalogQuery qo ){
        //吐司操作
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size() - 1);


        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);

        //根据分类的catalogId 去查出这个分类id下的
        return "destination/survey";
    }


    @RequestMapping("survey")
    public  Object survey(Model model, @ModelAttribute("qo")StrategyCatalogQuery qo ){
        //catalogs 根据destId去查出有什么分类
        List<StrategyCatalog> catalogs = strategyCatalogService.queryByDestId(qo.getDestId());
        model.addAttribute("catalogs",catalogs);

        //catalog  根据分类的id去查选中的是哪个分类 用来回显的
        StrategyCatalog catalog = strategyCatalogService.selectOne(qo.getCatalogId());
        model.addAttribute("catalog",catalog);

        //detail
            //文章 显示出文章
        List<StrategyDetail> details = catalog.getDetails();
        if (details!=null){
            StrategyDetail strategyDetail = details.get(0);
            //获得相对应的内容
            //根据传过来的destid来获得相当于应的攻略分类还有内容
            strategyDetail.setStrategyContent(strategyDetailService.getContentById(strategyDetail.getId()));//返回来的是Conte内容
            model.addAttribute("detail",strategyDetail);
        }
        //下面的内容

        return "destination/surveyTpl";
    }
    //travels
    @RequestMapping("travels")
    public  Object travels(Model model, @ModelAttribute("qo")TravelQuery qo ){
        //todo  查出所有的游记

        //条件1  多少月份
        //条件2 有多少工资
        //根据地店查出该地点的游记
        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "destination/travelTpl";
    }
}
